# A VIOLENCE FIGHT IT SHALL BE!!!!!
encounter:
.asciiz "\nA hostile "

attack:
.asciiz " attacks you"

initiative_fail:
.asciiz "\nEnemy strikes first!"

initiative_success:
.asciiz "\nYou strike first!"

enemy_hits:
.asciiz "\nEnemy strikes you for "

player_hits:
.asciiz "\nYou strike the enemy for "

damage:
.asciiz " damage."

enemy_misses:
.asciiz "\nEnemy misses!"

player_misses:
.asciiz "\nYou missed!"

death:
.asciiz "\nYou have been slain."

victory:
.asciiz "\nYou have slain the enemy."

combat_menu:
.asciiz "\nWhat would you like to do?\n1. Attack 1\n2. Attack 2\n3. Attack 3\n4. Attack 4"

# define possible enemy classes
# enemyclass 1
random_enemyclass_one:
.asciiz "Tiger"
random_enemyclass_one_hp:
.word 0x00000045
# enemyclass 2
random_enemyclass_two:
.asciiz "Ocelot"
random_enemyclass_two_hp:
.word 0x00000045
# enemyclass 3
random_enemyclass_three:
.asciiz "Cougar"
random_enemyclass_three_hp:
.word 0x00000045
# enemyclass 4
random_enemyclass_four:
.asciiz "Lynx"
random_enemyclass_four_hp:
.word 0x00000045

# this probably won't work, will need to figure it out
#current_enemyclass:
#.asciiz "MISSINGNO."
current_enemyclass_hp:
.word 0x00000001

# define player level characteristics (hp)
# 1
playerlevel_one_hp:
.word 0x00000045
# 2
playerlevel_two_hp:
.word 0x00000055
# 3
playerlevel_three_hp:
.word 0x00000065
# 4
playerlevel_four_hp:
.word 0x00000075

current_playerlevel_hp:
.word 0x00000001

# define player attacks
player_hitchance_one:
.word 0x00000000
player_basedamage_one:
.word 0x00000000
player_hitchance_two:
.word 0x00000000
player_basedamage_two:
.word 0x00000000
player_hitchance_three:
.word 0x00000000
player_basedamage_three:
.word 0x00000000
player_hitchance_four:
.word 0x00000000
player_basedamage_four:
.word 0x00000000

player_hitchance:
.word 0x00000000
player_basedamage:
.word 0x00000000

# are we going to war?!
combat_coinflip:
	push $31
	jal gen_random_one
	nop
	move $t0, $v0
	li $t1, 0x00000001
	# if it rolled a one, begin battle encounter
	beq $t0, $t1, select_playerlevel
	nop
	pop $31
	jr $31
	nop
# establish player characteristics
select_playerlevel:
	# take in current playerlevel from game, using $s7
	li $t0, $s7
	# player level 1?
	li $t1, 0x00000001
	beq $t0, $t1, select_playerlevel_one
	nop
	# player level 2?
	li $t1, 0x00000002
	beq $t0, $t1, select_playerlevel_two
	nop
	# player level 3?
	li $t1, 0x00000003
	beq $t0, $t1, select_playerlevel_three
	nop
	# player level 4?
	li $t1, 0x00000004
	beq $t0, $t1, select_playerlevel_four
	nop

# for level, assign current hp
select_playerlevel_one:
	li $t4, playerlevel_one_hp
	li $t5, current_playerlevel_hp
	sw $t5, 0($t4)
	j select_enemyclass
	nop	
select_playerlevel_two:
	li $t4, playerlevel_two_hp
	li $t5, current_playerlevel_hp
	sw $t5, 0($t4)
	j select_enemyclass
	nop
select_playerlevel_three:
	li $t4, playerlevel_three_hp
	li $t5, current_playerlevel_hp
	sw $t5, 0($t4)
	j select_enemyclass
	nop
select_playerlevel_four:
	li $t4, playerlevel_four_hp
	li $t5, current_playerlevel_hp
	sw $t5, 0($t4)
	j select_enemyclass
	nop
	
# WHO DARES TO RAISE HIS HAND AGAINST ME?
select_enemyclass:
	jal gen_random_f
	nop
	move $t2, $v0
	# is it class 1?
	li $t3, 0x00000001
	beq $t2, $t3, select_enemyclass_one
	nop
	# is it class 2?
	li $t3, 0x00000002
	beq $t2, $t3, select_enemyclass_two
	nop
	# is it class 3?
	li $t3, 0x00000003
	beq $t2, $t3, select_enemyclass_three
	nop
	# is it class 4?
	li $t3, 0x00000004
	beq $t2, $t3, select_enemyclass_four
	nop
	# none of these? ok try again
	# note: this could potentially be problematic.
	# if this is the "right" number of instructions
	# gen_random_f may never select 1-4
	j select_enemyclass
	nop

# if class one, set current enemy to class one's characteristics
select_enemyclass_one:
	li $t4, random_enemyclass_one_hp
	li $t5, current_enemyclass_hp
	sw $t5, 0($t4)
	# display combat-begin string
	li $a0, encounter
	jal libplp_uart_write_string
	nop
	li $a0, random_enemyclass_one
	jal libplp_uart_write_string
	nop
	li $a0, attack
	jal libplp_uart_write_string
	nop
	# begin battle
	j round_one_fight
	nop
# if class two, set current enemy to class two's characteristics
select_enemyclass_two:
	li $t4, random_enemyclass_two_hp
	li $t5, current_enemyclass_hp
	sw $t5, 0($t4)
	# display combat-begin string
	li $a0, encounter
	jal libplp_uart_write_string
	nop
	li $a0, random_enemyclass_two
	jal libplp_uart_write_string
	nop
	li $a0, attack
	jal libplp_uart_write_string
	nop
	# begin battle
	j round_one_fight
	nop
# if class three, set current enemy to class three's characteristics
select_enemyclass_three:
	li $t4, random_enemyclass_three_hp
	li $t5, current_enemyclass_hp
	sw $t5, 0($t4)
	# display combat-begin string
	li $a0, encounter
	jal libplp_uart_write_string
	nop
	li $a0, random_enemyclass_three
	jal libplp_uart_write_string
	nop
	li $a0, attack
	jal libplp_uart_write_string
	nop
	# begin battle
	j round_one_fight
	nop
# if class four, set current enemy to class four's characteristics
select_enemyclass_four:
	li $t4, random_enemyclass_four_hp
	li $t5, current_enemyclass_hp
	sw $t5, 0($t4)
	# display combat-begin string
	li $a0, encounter
	jal libplp_uart_write_string
	nop
	li $a0, random_enemyclass_four
	jal libplp_uart_write_string
	nop
	li $a0, attack
	jal libplp_uart_write_string
	nop
	# begin battle
	j round_one_fight
	nop

# BATTLE WAR BEGIN
round_one_fight:
	# load current characteristics
	# enemy hp to $s0
	li $s0, current_enemyclass_hp
	# player hp to $s1
	li $s1, current_player_hp
	# roll for initiative
	jal gen_random_one
	nop
	move $t0, $v0
	li $t1, 0x00000001
	beq $t0, $t1, enemy_turn_begin
	nop
	j player_turn_begin
	nop

# OH NOES ENEMY HITS FIRST
enemy_turn_begin:
	li $a0, initiative_fail
	jal libplp_uart_write_string
	nop
	j enemy_hit
	nop

# GET 'IM
player_turn_begin:
	li $a0, initiative_success
	jal libplp_uart_write_string
	nop
	j player_turn
	nop

# does the enemy hit you
enemy_hit:
	li $t0, enemy_hitchance
	jal gen_random_f
	nop
	move $t1, $a0
	# if roll is > hitchance, call it a hit
	# jump to calculate damage
	slt $t2, $t0, $t1
	bne $t2, $0, enemy_damage
	nop
	# otherwise, call it a miss
	li $a0, enemy_misses
	jal libplp_uart_write_string
	nop
	# progress to player turn
	j player_turn
	nop	

# if so, how badly does it hurt
# this is currently set to hit for
# basedmg + 1d15
enemy_damage:	
	li $t0, enemy_basedamage
	jal gen_random_f
	nop
	move $t1, $a0
	# calc total damage
	addu $t2, $t0, $t1
	# subtract from player health, store
	li $t3, current_playerlevel_hp
	subu $t4, $t3, $t2
	sw $t4, 0($t3)
	# display result to user
	li $a0, enemy_hits
	push $t2
	jal libplp_uart_write_string
	nop
	pop $t2
	li $a0, $t2
	jal libplp_uart_write_value_b2
	nop
	li $a0, damage
	jal libplp_uart_write_string
	nop
	# did the enemy kill you?
	# if 0 < health, set 1
	slt $t1, $0, $t3
	beq $0, $t1, player_death
	nop
	# if not, you get to fight back
	j player_turn
	nop
player_turn:
	li $a0, combat_menu
	jal libplp_uart_write_string
	nop
	jal libplp_uart_read
	nop
	move $t0, $v0
	li $t1, 0x00000031
	beq $t0, $t1, player_attack_one
	nop
	li $t1, 0x00000032
	beq $t0, $t1, player_attack_two
	nop
	li $t1, 0x00000033
	beq $t0, $t1, player_attack_three
	nop
	li $t1, 0x00000034
	beq $t0, $t1, player_attack_four
	nop
	# idiot didnt enter 1-4, do it again
	j player_turn
	nop

player_attack_one:
	# load specific attack values
	li $t4, player_hitchance_one
	li $t5, player_hitchance
	sw $t5, 0($t4)		
	li $t4, player_basedamage_one
	li $t5, player_basedamage
	sw $t5, 0($t4)
	j player_hit
	nop
player_attack_two:
	# load specific attack values
	li $t4, player_hitchance_two
	li $t5, player_hitchance
	sw $t5, 0($t4)		
	li $t4, player_basedamage_two
	li $t5, player_basedamage
	sw $t5, 0($t4)
	j player_hit
	nop
player_attack_three:
	# load specific attack values
	li $t4, player_hitchance_three
	li $t5, player_hitchance
	sw $t5, 0($t4)		
	li $t4, player_basedamage_three
	li $t5, player_basedamage
	sw $t5, 0($t4)
	j player_hit
	nop
player_attack_four:
	# load specific attack values
	li $t4, player_hitchance_four
	li $t5, player_hitchance
	sw $t5, 0($t4)		
	li $t4, player_basedamage_four
	li $t5, player_basedamage
	sw $t5, 0($t4)
	j player_hit
	nop

# do you hit the enemy
player_hit:
	li $t0, player_hitchance
	jal gen_random_f
	nop
	move $t1, $a0
	# if roll is > hitchance, call it a hit
	# jump to calculate damage
	slt $t2, $t0, $t1
	bne $t2, $0, player_damage
	nop
	# otherwise, call it a miss
	li $a0, player_misses
	jal libplp_uart_write_string
	nop
	# progress to enemy turn
	j enemy_turn
	nop	

# if so, how badly does it hurt
# this is currently set to hit for
# basedmg + 2d15
player_damage:
	li $t0, player_basedamage
	jal gen_random_f
	nop
	move $t1, $a0
	# calc damage + 1d15
	addu $t2, $t0, $t1
	jal gen_random_f
	nop
	move $t1, $a0
	# calc damage + 1d15
	addu $t2, $t2, $t1
	# subtract from player health, store
	li $t3, current_enemyclass_hp
	subu $t4, $t3, $t2
	sw $t4, 0($t3)
	# display result to user
	li $a0, player_hits
	push $t2
	jal libplp_uart_write_string
	nop
	pop $t2
	li $a0, $t2
	jal libplp_uart_write_value_b2
	nop
	li $a0, damage
	jal libplp_uart_write_string
	nop
	# did you kill the enemy?
	# if 0 < health, set 1
	slt $t1, $0, $t3
	beq $0, $t1, return_to_story
	nop
	j enemy_hit
	nop


return_to_story:
	li $a0, victory
	jal libplp_uart_write_string
	nop
	# we pushed return register during the coinflip
	pop $31
	jr $31
	nop

player_death:
	li $a0, death
	jal libplp_uart_write_string
	nop
